﻿using DataAchive.Base;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace DataAchive.Model.ViewModels
{
    public class InfoViewModel : ViewModelBase
    {
        public InfoViewModel()
        {
            SetComboxItems();
        }

        #region SiteInfoWindow
        /// <summary>
        /// 查询数据
        /// </summary>
        Page<ObservableCollection<InfoData>> _infoData = new Page<ObservableCollection<InfoData>>();
        public Page<ObservableCollection<InfoData>> InfoDataList
        {

            get { return _infoData; }
            set
            {
                _infoData = value;
                OnPropertyChanged(nameof(InfoDataList));
            }
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        ObservableCollection<InfoData> _infoAllData = new ObservableCollection<InfoData>();
        public ObservableCollection<InfoData> InfoAllDataList
        {

            get { return _infoAllData; }
            set
            {
                _infoAllData = value;
                OnPropertyChanged(nameof(InfoAllDataList));
            }
        }

        private InfoData _infoQuery;
        public InfoData InfoQuery
        {
            get { return _infoQuery; }
            set
            {
                _infoQuery = value;
                OnPropertyChanged(nameof(InfoQuery));
            }
        }

        public class InfoNgData
        {
            [Description("条码")]
            public string Lot_Id { get; set; }

        }

        public class InfoData : InfoNgData
        {
            [Description("ID")]
            public int? ID { get; set; }
            [Description("类型")]
            public string Classify { get; set; }
            [Description("X坐标")]
            public double? PX { get; set; }
            [Description("Y坐标")]
            public double? PY { get; set; }
            [Description("宽度")]
            public double? Width { get; set; }
            [Description("高度")]
            public double? Height { get; set; }
            [Description("缺陷ID")]
            public int? NG_Id { get; set; }
            [Description("创建者")]
            public string Creator { get; set; }
            [Description("位置")]
            public string Cell_Location { get; set; }
        }
        #endregion


        ObservableCollection<MultiCbxBaseData> _ItemsData = new ObservableCollection<MultiCbxBaseData>();
        public ObservableCollection<MultiCbxBaseData> ItemsData
        {

            get { return _ItemsData; }
            set
            {
                _ItemsData = value;
                OnPropertyChanged();
            }
        }





        private void SetComboxItems()
        {
            ObservableCollection<MultiCbxBaseData> items = new ObservableCollection<MultiCbxBaseData>();
            PropertyInfo[] propertyInfos = typeof(InfoData).GetProperties();
            if (propertyInfos.Length > 0)
            {
                MultiCbxBaseData selectAll = new MultiCbxBaseData() { ID = -1, ViewName = "全选" };
                selectAll.ISCheckChanged += SelectAll_Checked;
                items.Add(selectAll);
                foreach (var propertyInfo in propertyInfos)
                {
                    DescriptionAttribute descriptionAttribute = propertyInfo.GetCustomAttribute<DescriptionAttribute>();
                    string name = descriptionAttribute == null ? propertyInfo.Name : descriptionAttribute.Description;
                    items.Add(new MultiCbxBaseData() { ViewName = name, IsCheck = true });
                }
                ItemsData = items;
            }
        }

        private void SelectAll_Checked(object sender, EventArgs e)
        {
            var list = ItemsData?.ToList();
            if (list != null && list.Any())
            {
                MultiCbxBaseData allCheck = (MultiCbxBaseData)sender;
                foreach (var item in list)
                {
                    if (item.ID == -1)
                        continue;
                    item.IsCheck = allCheck.IsCheck;
                }
            }
        }
    }
}
